﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Client.Base;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmAddPayType : FormBase
    {
        public ControlClassModeEnum Flag { get; set; }

        private DBEntityPayType _payType;

        public frmAddPayType()
        {
            InitializeComponent();
        }

        public frmAddPayType(DBEntityPayType entity)
        {
            InitializeComponent();
            this._payType = entity;
        }

        private bool FormValidate()
        {
            string error = string.Empty;
            if (txtName.Text.Trim() == string.Empty)
            {
                error = "请输入支付方式名称。";
                MsgBox.MsgInfo(error);
                this.SetErrorControl(txtName, error);
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool bFlag = true;
            string error = string.Empty;
            if (FormValidate() == false)
                return;

            if (Flag == ControlClassModeEnum.emInsert)
            {
                _payType = new DBEntityPayType();
                _payType.PName = txtName.Text;
                _payType.PMemo = txtMemo.Text;
                bFlag = BussinessProxyManager.Instance.InsertPayType(_payType, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("新增支付方式失败\r\n" + error);
                    return;
                }
                else
                {
                    MsgBox.MsgInfo("新增支付方式成功。");
                    this.DialogResult = DialogResult.OK;
                }
            }
            else if (Flag == ControlClassModeEnum.emEdit)
            {
                _payType.PName = txtName.Text;
                _payType.PMemo = txtMemo.Text;
                bFlag = BussinessProxyManager.Instance.UpdatePayType(_payType, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("修改支付方式失败\r\n" + error);
                    return;
                }
                else
                {
                    MsgBox.MsgInfo("修改支付方式成功。");
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAddPayType_Shown(object sender, EventArgs e)
        {
            if (Flag == ControlClassModeEnum.emEdit && _payType != null)
            {
                this.Text = "修改支付方式";
                this.txtName.Text = _payType.PName;
                this.txtMemo.Text = _payType.PMemo;
            }
        }


    }
}
